package com.example.work4.http

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Message
import android.util.Log
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.work4.TestActivity
import com.example.work4.adapter.MomentsAdapter
import com.example.work4.http.BaseRetrofit.get
import com.example.work4.common.FIN
import com.example.work4.http.body.CommentSaveBody
import com.example.work4.http.body.MomentSaveBody
import com.example.work4.http.body.MomentSelectBody
import com.example.work4.http.data.MomentPageData
import com.example.work4.http.imp.MomentRetrofit
import com.example.work4.po.Comment
import com.example.work4.po.Moments
import com.example.work4.po.ResultBean
import com.example.work4.po.SSOInfo
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MomentRetrofitImp (){
    var handler:Handler?=null

    var context:Context?=null

    var retrofit = get(FIN.BaseUrl)

    var getMoment = retrofit.create(MomentRetrofit::class.java)

    fun getMyMoment()
    {
        SSOInfo.token?.let { getMoment.getMyMoments(it) }?.enqueue(object :
            Callback<ResultBean<List<Moments>>>
        {
            override fun onResponse(
                call: Call<ResultBean<List<Moments>>>,
                response: Response<ResultBean<List<Moments>>>
            ) {
                var code = response.body()?.code
                response.body()?.msg?.let { it2 -> Log.i("e", it2) }
                if (code == 0) {
                    var list: List<Moments>? = response.body()?.data
                    var message=Message()
                    message.what=FIN.MESSAGEOFMOMENTS
                    message.obj=list
                    handler?.sendMessage(message)
                } else {
                    Toast.makeText(context, "获取失败刷新再次获取！", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<ResultBean<List<Moments>>>, t: Throwable) {

                System.out.println("x")
            }

        })
    }

    fun saveMoment(body: MomentSaveBody) {
        SSOInfo.token?.let { getMoment.saveMoment(it, body) }?.enqueue(object :
            Callback<ResultBean<Void>> {
            override fun onResponse(
                call: Call<ResultBean<Void>>,
                response: Response<ResultBean<Void>>
            ) {
                var code = response.body()?.code
                response.body()?.msg?.let { it2 -> Log.i("e", it2) }
                if (code == 0) {
                    val intent = Intent();
                    context?.let {
                        intent.setClass(it,TestActivity::class.java)
                        it.startActivity(intent)
                    }
                } else {
                    Toast.makeText(context, "上传失败！", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<ResultBean<Void>>, t: Throwable) {
                TODO("Not yet implemented")
            }

        })
    }

    fun addComment(body: CommentSaveBody) {
        SSOInfo.token?.let { getMoment.addComment(it, body) }?.enqueue(object :
            Callback<ResultBean<Void>> {
            override fun onResponse(
                call: Call<ResultBean<Void>>,
                response: Response<ResultBean<Void>>
            ) {
                var code = response.body()?.code
                response.body()?.msg?.let { it2 -> Log.i("e", it2) }
                if (code == 0) {
                    Toast.makeText(context, "发送成功！", Toast.LENGTH_SHORT).show()
                    queryCommentByMomentId(body.momentId)
                } else {
                    Toast.makeText(context, "发送失败！", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<ResultBean<Void>>, t: Throwable) {
                TODO("Not yet implemented")
            }

        })
    }

    fun getAllFriendsMoments(body: MomentSelectBody)
    {
        SSOInfo.token?.let { getMoment.getMoments(it,body) }?.enqueue(object :
            Callback<ResultBean<MomentPageData>>
        {
            override fun onResponse(
                call: Call<ResultBean<MomentPageData>>,
                response: Response<ResultBean<MomentPageData>>
            ) {
                var code = response.body()?.code
                response.body()?.msg?.let { it2 -> Log.i("e", it2) }
                if (code == 0) {
                    var list: MomentPageData? = response.body()?.data
                    var message=Message()
                    message.what=FIN.MESSAGEOFMOMENTS
                    message.obj=list
                    handler?.sendMessage(message)
                } else {
                    Toast.makeText(context, "没有关注任何用户！", Toast.LENGTH_SHORT).show()
                }
            }
            override fun onFailure(call: Call<ResultBean<MomentPageData>>, t: Throwable) {

                System.out.println("x")
            }

        })
    }

    fun getMomentsById(id:Long,adapter:MomentsAdapter,view:RecyclerView)
    {
        val body = MomentSelectBody()
        body.curId = -1
        body.size = 100
        body.userId = id.toInt()
        SSOInfo.token?.let { getMoment.getMomentsById(it,body) }?.enqueue(object :
            Callback<ResultBean<MomentPageData>>
        {
            override fun onResponse(
                call: Call<ResultBean<MomentPageData>>,
                response: Response<ResultBean<MomentPageData>>
            ) {
                var code = response.body()?.code
                response.body()?.msg?.let { it2 -> Log.i("e", it2) }
                if (code == 0) {
                    var list: MomentPageData? = response.body()?.data
                    adapter.data = list?.items as ArrayList<Moments>;
                    view.adapter = adapter;
                } else {
                    Toast.makeText(context, "获取失败刷新再次获取！", Toast.LENGTH_SHORT).show()
                }
            }
            override fun onFailure(call: Call<ResultBean<MomentPageData>>, t: Throwable) {

                System.out.println("x")
            }

        })
    }

    fun queryCommentByMomentId(id:Long) {
        SSOInfo.token?.let { getMoment.queryCommentByMomentId(it, id) }?.enqueue(object :
            Callback<ResultBean<List<Comment>>> {
            override fun onResponse(call: Call<ResultBean<List<Comment>>>,
                                    response: Response<ResultBean<List<Comment>>>) {
                val code = response.body()?.code
                response.body()?.msg?.let { it2 -> Log.i("e", it2) }
                if (code == 0) {
                    val list: List<Comment>? = response.body()?.data
                    val message = Message()
                    message.what = FIN.UPDATECOMMENTLIST
                    message.obj = list
                    handler?.sendMessage(message)
                } else {
                    Toast.makeText(context, "获取失败刷新再次获取！", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<ResultBean<List<Comment>>>, t: Throwable) {
                TODO("Not yet implemented")
            }

        })
    }

    fun queryMomentsInDis(lon:Double,lat:Double,dis:Double)
    {
        SSOInfo.token?.let { getMoment.queryMomentsInDis(it,lon,lat,dis) }?.enqueue(object :
            Callback<ResultBean<MomentPageData>>
        {
            override fun onResponse(
                call: Call<ResultBean<MomentPageData>>,
                response: Response<ResultBean<MomentPageData>>
            ) {
                var code = response.body()?.code
                response.body()?.msg?.let { it2 -> Log.i("e", it2) }
                if (code == 0) {
                    var list: MomentPageData? = response.body()?.data
                    var message=Message()
                    message.what=FIN.MESSAGEOFMOMENTS
                    message.obj=list
                    handler?.sendMessage(message)
                } else {
                    Toast.makeText(context, "获取失败刷新再次获取！", Toast.LENGTH_SHORT).show()
                }
            }
            override fun onFailure(call: Call<ResultBean<MomentPageData>>, t: Throwable) {

                System.out.println("x")
            }

        })
    }
}